#!/bin/bash

# 客户管理模块测试运行脚本
# 使用方法：./run-customer-tests.sh [选项]
#
# 选项：
#   all          - 运行所有测试（默认）
#   repository   - 仅运行 Repository 测试
#   service      - 仅运行 Service 测试
#   controller   - 仅运行 Controller 测试
#   integration  - 仅运行集成测试
#   coverage     - 运行测试并生成覆盖率报告

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}  客户管理模块测试运行脚本${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""

# 获取测试类型参数
TEST_TYPE=${1:-all}

case $TEST_TYPE in
  repository)
    echo -e "${YELLOW}运行 Repository 层测试...${NC}"
    ./mvnw test -Dtest=CustomerRepositoryTest
    ;;
  
  service)
    echo -e "${YELLOW}运行 Service 层测试...${NC}"
    ./mvnw test -Dtest=CustomerServiceTest
    ;;
  
  controller)
    echo -e "${YELLOW}运行 Controller 层测试...${NC}"
    ./mvnw test -Dtest=CustomerControllerTest
    ;;
  
  integration)
    echo -e "${YELLOW}运行集成测试...${NC}"
    ./mvnw test -Dtest=CustomerIntegrationTest
    ;;
  
  coverage)
    echo -e "${YELLOW}运行所有测试并生成覆盖率报告...${NC}"
    ./mvnw clean test jacoco:report
    echo ""
    echo -e "${GREEN}✓ 测试完成！${NC}"
    echo -e "${GREEN}✓ 覆盖率报告已生成：target/site/jacoco/index.html${NC}"
    ;;
  
  all)
    echo -e "${YELLOW}运行所有客户管理模块测试...${NC}"
    ./mvnw test -Dtest=Customer*Test
    ;;
  
  *)
    echo -e "${RED}错误：未知的测试类型 '$TEST_TYPE'${NC}"
    echo ""
    echo "使用方法："
    echo "  $0 [选项]"
    echo ""
    echo "选项："
    echo "  all          - 运行所有测试（默认）"
    echo "  repository   - 仅运行 Repository 测试"
    echo "  service      - 仅运行 Service 测试"
    echo "  controller   - 仅运行 Controller 测试"
    echo "  integration  - 仅运行集成测试"
    echo "  coverage     - 运行测试并生成覆盖率报告"
    exit 1
    ;;
esac

# 检查测试结果
if [ $? -eq 0 ]; then
  echo ""
  echo -e "${GREEN}========================================${NC}"
  echo -e "${GREEN}  ✓ 所有测试通过！${NC}"
  echo -e "${GREEN}========================================${NC}"
else
  echo ""
  echo -e "${RED}========================================${NC}"
  echo -e "${RED}  ✗ 测试失败！${NC}"
  echo -e "${RED}========================================${NC}"
  exit 1
fi

